////////////////////////////////////////////////////////////////////// // LibFile: beziers.scad // Bezier functions and modules. // To use, add the following lines to the beginning of your file: // ``` // include // use // ``` ////////////////////////////////////////////////////////////////////// /* BSD 2-Clause License Copyright (c) 2017, Revar Desmera All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ include use use use // Section: Terminology // **Polyline**: A series of points joined by straight line segements. // // **Bezier Curve**: A mathematical curve that joins two endpoints, following a curve determined by one or more control points. // // **Endpoint**: A point that is on the end of a bezier segment. This point lies on the bezier curve. // // **Control Point**: A point that influences the shape of the curve that connects two endpoints. This is often *NOT* on the bezier curve. // // **Degree**: The number of control points, plus one endpoint, needed to specify a bezier segment. Most beziers are cubic (degree 3). // // **Bezier Segment**: A list consisting of an endpoint, one or more control points, and a final endpoint. The number of control points is one less than the degree of the bezier. A cubic (degree 3) bezier segment looks something like: // `[endpt1, cp1, cp2, endpt2]` // // **Bezier Path**: A list of bezier segments flattened out into a list of points, where each segment shares the endpoint of the previous segment as a start point. A cubic Bezier Path looks something like: // `[endpt1, cp1, cp2, endpt2, cp3, cp4, endpt3]` // **NOTE**: A bezier path is *NOT* a polyline. It is only the points and controls used to define the curve. // // **Bezier Patch**: A surface defining grid of (N+1) by (N+1) bezier points. If a Bezier Segment defines a curved line, a Bezier Patch defines a curved surface. // // **Bezier Surface**: A surface defined by a list of one or more bezier patches. // // **Spline Steps**: The number of straight-line segments to split a bezier segment into, to approximate the bezier curve. The more spline steps, the closer the approximation will be to the curve, but the slower it will be to generate. Usually defaults to 16. // Section: Segment Functions // Function: bez_point() // Usage: // bez_point(curve, u) // Description: // Formula to calculate points on a bezier curve. The degree of // the curve, N, is one less than the number of points in `curve`. // Arguments: // curve = The list of endpoints and control points for this bezier segment. // u = The proportion of the way along the curve to find the point of. 0<=`u`<=1 // Example(2D): Quadratic (Degree 2) Bezier. // bez = [[0,0], [30,30], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // translate(bez_point(bez, 0.3)) color("red") sphere(1); // Example(2D): Cubic (Degree 3) Bezier // bez = [[0,0], [5,35], [60,-25], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // translate(bez_point(bez, 0.4)) color("red") sphere(1); // Example(2D): Degree 4 Bezier. // bez = [[0,0], [5,15], [40,20], [60,-15], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // translate(bez_point(bez, 0.8)) color("red") sphere(1); function bez_point(curve,u)= (len(curve) <= 1) ? curve[0] : bez_point( [for(i=[0:len(curve)-2]) curve[i]*(1-u)+curve[i+1]*u], u ); // Function: bez_tangent() // Usage: // bez_tangent(curve, u) // Description: // Calculate the tangent vector at a point on a bezier curve. // Arguments: // curve = The list of endpoints and control points for this bezier segment. // u = The proportion of the way along the curve to find the point of. 0<=`u`<=1 // Example(2D): Cubic (degree 3) bezier tangent // bez = [[0,0], [5,35], [60,-25], [80,0]]; // point = bez_point(bez, 0.2); // tangent = bez_tangent(bez, 0.2) * 10; // trace_bezier(bez, N=len(bez)-1); // color("red") translate(point) extrude_from_to(-tangent, tangent) circle(d=.5); function bez_tangent(curve, u) = ( len(curve) <= 2 ? normalize(u * (curve[1] - curve[0])) : bez_tangent([ for(i=[0:len(curve)-2]) curve[i] * (1 - u) + curve[i + 1] * u ], u) ); // Function: bezier_segment_closest_point() // Usage: // bezier_segment_closest_point(bezier,pt) // Description: // Finds the closest part of the given bezier segment to point `pt`. // The degree of the curve, N, is one less than the number of points in `curve`. // Returns `u` for the shortest position on the bezier segment to the given point `pt`. // Arguments: // curve = The list of endpoints and control points for this bezier segment. // pt = The point to find the closest curve point to. // max_err = The maximum allowed error when approximating the closest approach. // Example(2D): // pt = [40,15]; // bez = [[0,0], [20,40], [60,-25], [80,0]]; // u = bezier_segment_closest_point(bez, pt); // trace_bezier(bez, N=len(bez)-1); // color("red") translate(pt) sphere(r=1); // color("blue") translate(bez_point(bez,u)) sphere(r=1); function bezier_segment_closest_point(curve, pt, max_err=0.01, u=0, end_u=1) = let( steps = len(curve)*3, path = [for (i=[0:1:steps]) let(v=(end_u-u)*(i/steps)+u) [v, bez_point(curve, v)]], bracketed = concat([path[0]], path, [path[len(path)-1]]), minima_ranges = [ for (i = [1:1:len(bracketed)-1]) let( d1=norm(bracketed[i ][1]-pt), d2=norm(bracketed[i+1][1]-pt), d3=norm(bracketed[i+2][1]-pt) ) if(d2<=d1 && d2<=d3) [ bracketed[i][0], bracketed[i+2][0] ] ] ) len(minima_ranges)>1? ( let( min_us = [ for (minima = minima_ranges) bezier_segment_closest_point(curve, pt, max_err=max_err, u=minima.x, end_u=minima.y) ], dists = [for (v=min_us) norm(bez_point(curve,v)-pt)], min_i = min_index(dists) ) min_us[min_i] ) : let( minima = minima_ranges[0], p1 = bez_point(curve, minima.x), p2 = bez_point(curve, minima.y) ) norm(p1-p2)= len(path))? ( let(curve = select(path, min_seg*N, (min_seg+1)*N)) [min_seg, bezier_segment_closest_point(curve, pt, max_err=max_err)] ) : ( let( curve = select(path,seg*N,(seg+1)*N), u = bezier_segment_closest_point(curve, pt, max_err=0.05), dist = norm(bez_point(curve, u)-pt), mseg = (min_dist==undef || dist= len(patches))? [vertices, faces] : bezier_patch(patches[i], splinesteps=splinesteps, vertices=vertices, faces=faces), vnf2 = (i >= len(tris))? vnf : bezier_triangle(tris[i], splinesteps=splinesteps, vertices=vnf[0], faces=vnf[1]) ) (i >= len(patches) && i >= len(tris))? vnf2 : bezier_surface(patches=patches, tris=tris, splinesteps=splinesteps, i=i+1, vertices=vnf2[0], faces=vnf2[1]); // Section: Bezier Surface Modules // Module: bezier_polyhedron() // Useage: // bezier_polyhedron(patches) // Description: // Takes a list of two or more bezier patches and attempts to make a complete polyhedron from them. // Arguments: // patches = A list of rectangular bezier patches. // tris = A list of triangular bezier patches. // vertices = Vertex list for additional non-bezier faces. Default: [] // faces = Additional non-bezier faces. Default: [] // splinesteps = Number of steps to divide each bezier segment into. Default: 16 // Example: // patch1 = [ // [[18,18,0], [33, 0, 0], [ 67, 0, 0], [ 82, 18,0]], // [[ 0,40,0], [ 0, 0, 20], [100, 0, 20], [100, 40,0]], // [[ 0,60,0], [ 0,100, 20], [100,100,100], [100, 60,0]], // [[18,82,0], [33,100, 0], [ 67,100, 0], [ 82, 82,0]], // ]; // patch2 = [ // [[18,18,0], [33, 0, 0], [ 67, 0, 0], [ 82, 18,0]], // [[ 0,40,0], [ 0, 0,-50], [100, 0,-50], [100, 40,0]], // [[ 0,60,0], [ 0,100,-50], [100,100,-50], [100, 60,0]], // [[18,82,0], [33,100, 0], [ 67,100, 0], [ 82, 82,0]], // ]; // bezier_polyhedron([patch1, patch2], splinesteps=8); module bezier_polyhedron(patches=[], tris=[], splinesteps=16, vertices=[], faces=[]) { sfc = bezier_surface(patches=patches, tris=tris, splinesteps=splinesteps, vertices=vertices, faces=faces); polyhedron(points=sfc[0], faces=sfc[1]); } // Module: trace_bezier_patches() // Usage: // trace_bezier_patches(patches, [size], [showcps], [splinesteps]); // trace_bezier_patches(tris, [size], [showcps], [splinesteps]); // trace_bezier_patches(patches, tris, [size], [showcps], [splinesteps]); // Description: // Shows the surface, and optionally, control points of a list of bezier patches. // Arguments: // patches = A list of rectangular bezier patches. // tris = A list of triangular bezier patches. // splinesteps = Number of steps to divide each bezier segment into. default=16 // showcps = If true, show the controlpoints as well as the surface. // size = Size to show control points and lines. // Example: // patch1 = [ // [[15,15,0], [33, 0, 0], [ 67, 0, 0], [ 85, 15,0]], // [[ 0,33,0], [33, 33, 50], [ 67, 33, 50], [100, 33,0]], // [[ 0,67,0], [33, 67, 50], [ 67, 67, 50], [100, 67,0]], // [[15,85,0], [33,100, 0], [ 67,100, 0], [ 85, 85,0]], // ]; // patch2 = [ // [[15,15,0], [33, 0, 0], [ 67, 0, 0], [ 85, 15,0]], // [[ 0,33,0], [33, 33,-50], [ 67, 33,-50], [100, 33,0]], // [[ 0,67,0], [33, 67,-50], [ 67, 67,-50], [100, 67,0]], // [[15,85,0], [33,100, 0], [ 67,100, 0], [ 85, 85,0]], // ]; // trace_bezier_patches(patches=[patch1, patch2], splinesteps=8, showcps=true); module trace_bezier_patches(patches=[], tris=[], size=1, showcps=false, splinesteps=16) { if (showcps) { for (patch = patches) { place_copies(flatten(patch)) color("red") sphere(d=size*2); color("cyan") for (i=[0:len(patch)-1], j=[0:len(patch[i])-1]) { if (i